/**
 * Created by xxx on 2017/7/12.
 */
'use strict';
import React, {
    Component
} from 'react'
import {
    Link,
    hashHistory
} from 'react-router'

import Header from './Common/Header'
import LoadingBox from './Common/LoadingBox'

import {
    HttpService,
    getLocalTime,
    Tips,
    AjaxUploadImgUrl,
    getClassList
} from '../Tools'

import albumDefault from '../Img/other/albumDefault.png'

/*
 *班级相册-教师、家长
 */
export default class ScoreDetail extends Component {
    constructor(props) {
        super(props);
        this.photoid = '';
        this.state = {
            albumList: [],
            classList: [],
            classid: '',
            dataLoad: 1,
            dataNull: 0
        }
    }
    componentWillMount() {
        if (sessionStorage.classid) {
            sessionStorage.albumclassid = sessionStorage.classid
        } else {
            getClassList(classList => {
                if (!sessionStorage.albumclassid) {
                    sessionStorage.albumclassid = classList[0].id
                    sessionStorage.albumclassName = classList[0].name
                }
                this.setState({
                    classList
                });
            })
        }
        this.setState({
            classid: sessionStorage.albumclassid
        });
        this.getAlbum();
    }
    componentDidMount() {
        if (document.getElementById('albumcls'))
            document.getElementById('albumcls').value = sessionStorage.albumclassid
    }

    //查询班级的相册第一张
    getAlbumPhoto(bookid, callback) {
        HttpService.query({
            url: '/jxt-nyxt/ajaxClassPhotoBookQuery.do',
            async: false,
            data: {
                page: 1,
                pagesize: 1,
                jybTicket: sessionStorage.ticket,
                method: 'ajaxJybJSON_001QryBookPhoto',
                bookid
            },
            success: (res => {
                callback(res)
            })
        })
    }

    //获取相册列表
    getAlbum() {
        // if (sessionStorage.albumclassid == '') {
        //     Tips('正在加载中...');
        //     setTimeout(() => {
        //         this.getAlbum()
        //     }, 200)
        //     return;
        // }
        this.setState({
            dataLoad: 1,
            dataNull: 0,
            albumList: []
        });
        HttpService.query({
            url: '/jxt-nyxt/ajaxClassPhotoBookQuery.do',
            data: {
                classid: sessionStorage.albumclassid,
                properties: 'photonum,createDate,candelete',
                jybTicket: sessionStorage.ticket,
                method: 'ajaxJybJSON_001QryClassPhotoBook'
            },
            success: (res => {
                if (res.code == 0) {
                    let albumList = res.rs;
                    albumList.map((item, index) => {
                        this.getAlbumPhoto(item.id, (data) => {
                            if (data && data.rs[0] && data.rs[0].fileId)
                                albumList[index].fileId = AjaxUploadImgUrl + data.rs[0].fileId
                        });
                    })
                    //setTimeout(() => {
                    this.setState({
                        dataLoad: 0,
                        albumList
                    });
                    //}, 200)
                    if (res.rs.length == 0)
                        this.setState({
                            dataNull: 1
                        })
                    return;
                }
                Tips(res.msg);
            })
        })
    }

    //选择班级
    sltClass(e) {
        let obj = e.currentTarget
        const clsName = obj.options[obj.selectedIndex].text
        sessionStorage.albumclassid = obj.value
        sessionStorage.albumclassName = clsName
        document.querySelector('.h_title span').innerHTML = clsName
        this.setState({
            dataLoad: 1,
            dataNull: 0,
            classid: sessionStorage.albumclassid
        })
        this.getAlbum();
    }
    //移动图片选择
    moveSlt(photoid, e) {
        if (this.props.params.id) {
            e.preventDefault();
            this.photoid = photoid;
            if (document.querySelector('.pjbcb_active'))
                document.querySelector('.pjbcb_active').className = ''
            e.currentTarget.className = 'pjbcb_active';
        }
    }
    //移动图片提交
    movePhoto(e) {
        if (this.photoid == '') {
            Tips('请选择移到的相册')
            return;
        } else {
            HttpService.query({
                url: '/jxt-nyxt/ajaxClassPhotoBookDealer.do',
                data: {
                    jybTicket: sessionStorage.ticket,
                    method: 'ajaxJybJSON_101MovePhoto',
                    classid: sessionStorage.albumclassid,
                    frombookid: this.props.params.id,
                    tobookid: this.photoid,
                    photoid: this.props.params.photoid
                },
                success: (res => {
                    if (res.code == 0) {
                        hashHistory.goBack(-1);
                    } else {
                        Tips(res.msg);
                    }
                })
            })
        }
    }
    render() {
        const {
            dataLoad,
            dataNull,
            albumList,
            classList,
            classid
        } = this.state;
        const albumTagList = albumList.map((item, index) => {
            if (this.props.params.id && this.props.params.id == item.id){
                return;
            }
            let bjimg = item.fileId;
            if (item.fileId == undefined){
                bjimg = albumDefault
            }else if(bjimg.indexOf(',')>-1){
                bjimg = bjimg.split(',')[0]
            }
            return (
                <li className="ablum_list">
                    <Link onClick={this.moveSlt.bind(this, item.id)} to={'/AlbumDetail/' + item.id + '/' + classid + '/' + encodeURIComponent(item.dir) + '/' + encodeURIComponent(item.dirremark
                        ? item.dirremark
                        : '无描述')}>
                        <span className='pjb_lbm' style={{
                            backgroundImage: 'url(' + bjimg + ')'
                        }}></span>
                        <span className="title">
                            <span className="title_content">{item.dir}</span>
                            <span className="photo_num">({item.photonum})</span>
                        </span>
                        <span className="content">{item.dirremark
                                ? item.dirremark
                                : '无描述'}</span>
                    </Link>
                </li>
            )
        })
        const classTagList = classList.map((item, index) => {
            return (
                <option value={item.id}>{item.name}</option>
            )
        })
        let publishLink = null;
        if (sessionStorage.userrole != '家长')
            publishLink = <Link className='h_link add_blue' to={'/AddAlbum/' + sessionStorage.albumclassid}/>
        if (this.props.params.id)
            publishLink = <a className='h_link color-blue' onClick={this.movePhoto.bind(this)}>移动</a>
        return (
            <div className='class_album'>
                <Header link={publishLink} title={sessionStorage.classid
                    ? '班级相册'
                    : null} appLink='1' appsign={this.props.params.id}>
                    <span  className={classList.length==1?'sigle':''}>{sessionStorage.albumclassName}</span><br/>
                    <select className="sltbox" id='albumcls' onChange={this.sltClass.bind(this)}>
                        {classTagList}
                    </select>
                </Header>

                <ul className="ablum_container clearfix">
                    <LoadingBox dataLoad={dataLoad} dataNull={dataNull}/> {albumTagList}
                </ul>
            </div>
        )
    }
}
